<?php
header("Content-type: text/html; charset=utf-8");
class Editor_model extends CI_Model{
	function __construct()
    {
        parent::__construct();
    }
	//出版社管理
	function publisher_view($sql){ //显示出版社页面的内容
		
		$query = $this->db->query($sql);
		return $query->result_array();
	}
	function publisher_file_view($id){ //显示修改页面的内容
		//$this->id = $id;
		$sql = "select * from publishers where id='{$id}'";
		$query = $this->db->query($sql);
		$row = $query->row_array();
		return $row;
	}
	function new_publisher(){ //新建出版社
		$sql = "select * from publishers where 1";
		$name = $this->input->post('name');
		$query = $this->db->query($sql);
		foreach($query->result_array() as $row){
			if($row['publishername']==$name){
				echo "<script language=javascript>
				alert('出版社已存在');
				window.location='".base_url()."editor_publisher/new_publisher';
				</script>";
				exit();
			}
		}
		$this->db->query("insert into publishers values('default','{$name}')");
		echo "<script language=javascript>
			alert('新建成功！');
			window.location='".base_url()."editor_publisher';
			</script>";
	}
	function modify_publisher(){ //修改
		$id = $this->input->post('id');
		$name = $this->input->post('name');
		$sql = "select * from publishers where id<>'{$id}'";
		$query = $this->db->query($sql);
		foreach($query->result_array() as $row){
			if($row['publishername']==$name){
				echo "<script language=javascript>
					alert('出版社已存在');
					window.location='".base_url()."editor_publisher/modify_publisher/".$id."';
					</script>";
				exit();
			}
		}
		$this->db->query("update publishers set publishername='{$name}' where id='{$id}'");
		echo "<script language=javascript>
			alert('修改成功！');
			window.location='".base_url()."editor_publisher';
			</script>";
	}
	function del_publisher($id){
		$sql = "delete from publishers where id='{$id}'";
		$this->db->query($sql);
		echo "<script language=javascript>
			alert('删除成功！');
			window.location='".base_url()."editor_publisher';
			</script>";
	}
	//绘本管理
	function books_view($sql){
		$query = $this->db->query($sql);
		return $query->result_array();
	}
	function checking($publisher){
		$res = $this->db->query("select id from publishers where publishername='{$publisher}'");
		if($row = $res->row_array()){
			$pid = $row['id'];
		}else{
			if($this->input->post('id')){
				echo "<script language=javascript>
						alert('出版社不存在');
						window.location='".base_url()."editor_books/modify_book/".$this->input->post('id')."';
						</script>";
						exit();
			}else{
				echo "<script language=javascript>
						alert('出版社不存在');
						window.location='".base_url()."editor_books/new_book';
						</script>";
						exit();
			}
			
		}
		//判断是否提交备注里的内容
		$source = $this->input->post('source');
		$destination = $this->input->post('destination');
		$language = $this->input->post('language');
		$description = $this->input->post('description');
		$note1 = $this->input->post('note1');
		$note2 = $this->input->post('note2');
		$note3 = $this->input->post('note3');
		$note4 = $this->input->post('note4');
		
		$source = ($source=='备注')?$note1:$source;
		$destination = ($destination=='备注')?$note2:$destination;
		$language = ($language=='备注')?$note3:$language;
		$description = ($description=='备注')?$note4:$description;
		return array(
			'pid'=>$pid,
			'source'=>$source,
			'destination'=>$destination,
			'language'=>$language,
			'description'=>$description
		);
	}
	function new_book($data){
		$title = $this->input->post('title');
		$query = $this->db->query('select * from books where 1');
		foreach($query->result_array() as $row){
			if($row['title']==$title){
			echo "<script language=javascript>
				alert('书名已存在');
				window.location='".base_url()."editor_books/new_book';
				</script>";
				exit();
			}
		}
		$this->db->query("insert into books values('default','','{$title}','{$data['source']}','{$data['destination']}','{$data['language']}','{$data['description']}','{$data['pid']}')");
		echo "<script language=javascript>
			alert('新建成功！');
			window.location='".base_url()."editor_books';
			</script>";
	}
	function book_file_view($id){ //显示修改页面的内容
		
		$sql = "select * from books left join publishers on publisher_id=publishers.id where books.id='{$id}'";
		$query = $this->db->query($sql);
		$row = $query->row_array();
		$options = array(
				//绘本来源列表
				'source'=>array(
					'实体书'=>'实体书',
					'源文件'=>'源文件',
					'实体书+源文件'=>'实体书+源文件'
				),
				//制作形式的列表
				'destination'=>array(
					'简单动画'=>'简单动画',
					'精致动画'=>'精致动画',
					'电子杂志'=>'电子杂志',
					'儿歌（手机版/线上版）'=>'儿歌（手机版/线上版）',
					'动画MTV书'=>'动画MTV书'
				),
				//语言的列表
				'language'=>array(
					'中'=>'中',
					'英'=>'英',
					'英|中'=>'英|中'
				),
				//状态列表
				'description'=>array(
					'完成并上线'=>'完成并上线',
					'初稿完成'=>'初稿完成',
					'版權洽談中'=>'版權洽談中'
				)
			);
		$data['source'] = in_array($row['source'],$options['source'])?$row['source']:'备注';
		$data['destination'] = in_array($row['destination'],$options['destination'])?$row['destination']:'备注';
		$data['description'] = in_array($row['description'],$options['description'])?$row['description']:'备注';
		$data['language'] = in_array($row['language'],$options['language'])?$row['language']:'备注';
		$data['row'] = $row;
		$data['id'] = $id;
		return $data;
	}
	
	function modify_book($id,$data){
		$title = $this->input->post('title');
		$res = $this->db->query("select * from books where id<>'{$id}'");
			foreach($res->result_array() as $row){
				if($row['title']==$title){
				echo "<script language=javascript>
					alert('书名已存在');
					window.location='".base_url()."editor_books/modify_book/".$id."';
					</script>";
					exit();
				}
			}
			$this->db->query("update books set title='{$title}',source='{$data['source']}',destination='{$data['destination']}',language='{$data['language']}',description='{$data['description']}',publisher_id='{$data['pid']}' where id='{$id}'");
			echo "<script language=javascript>
				alert('修改成功！');
				window.location='".base_url()."editor_books';
				</script>";
	}
	function del_book($id){
		$sql = "delete from books where id='{$id}'";
		$this->db->query($sql);
		echo "<script language=javascript>
			alert('删除成功！');
			window.location='".base_url()."editor_books';
			</script>";
	}
	function ajax($p,$p_name){
		if($p){
			$this->db->query("insert into publishers values('default','{$p}')");
			return "新出版社创建成功";
		}else{
			$res = $this->db->query("select id from publishers where publishername='{$p_name}'");
			if($row = $res->row_array()){
				return "a";
			}else{
				return "出版社不存在，是否添加？";
			}
		}

	}
	function autocomplete($q,$t){
		if (!$q) return;
			$res = $this->db->query("select title from books where title like '%$t%'");
			$items = array();
		foreach($res->result_array() as $row){
			$i++;
			$items[] =array('label'=>$row['title']);
			if($i>10)
				break;
		}
		return json_encode($items);
	}
}
?>